package com.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.student.dao.AdminMapper;
import com.student.entity.Admin;
import com.student.qpage.QPage;
import com.student.service.IAdminService;
import com.student.util.Result;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 管理员业务接口实现层
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {



    /**
     * 列表分页
     * @param qPage
     * @param obj
     * @return
     */
    @Override
    public IPage getList(QPage qPage, Admin obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<Admin>();
        IPage mapList =this.page(iPage,queryWrapper);
        return mapList;
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Override
    public Result insert(Admin obj) {
        obj.setCtime(new Date());
        int isOk=this.baseMapper.insert(obj);
        if(isOk==1){
            return Result.reOk("新增成功");
        }
        return Result.reError("新增失败");
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Override
    public Result updates(Admin obj) {
        int isOk=this.baseMapper.updateById(obj);
        if(isOk==1){
            return Result.reOk("修改成功");
        }
        return Result.reError("修改失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteId(Integer id) {
        Admin admin=this.getById(id);
        if(admin.getType()==1){
            return Result.reError("超级管理员不能删除");
        }
        int isOk=this.baseMapper.deleteById(id);
        if(isOk==1){
            return Result.reOk("删除成功");
        }
        return Result.reError("删除失败");
    }

    /**
     * 登录
     * @param account
     * @return
     */
    @Override
    public Admin login(String account,String pwd) {
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<Admin>();
        queryWrapper.eq("username",account);
        queryWrapper.eq("pwd",pwd);
        return this.getOne(queryWrapper);
    }
}
